package final_project;
/**
 * 充分利用BorderLayout、FlowLayout、BoxLayout等布局管理器以及相关组件，
 * 为上次上机作业设计一个界面，界面应该包含增加、查询和删除等按钮，
 * 可根据申请日期、IPC分类号和CPC分类号查询和删除相应专利文献（暂不实现具体功能）。
 * */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class PatentManagerUI extends JFrame {
    // 输入字段
    private JTextField applicationNoField;
    private JTextField applicationDateField;
    private JTextField ipcField;
    private JTextField cpcField;
    private JTextField cpcPositionField;
    private JTextField cpcValueField;

    // 数据存储
    private Corpus corpus = new Corpus();

    public PatentManagerUI() {
        initializeUI();
    }

    private void initializeUI() {
        setTitle("专利文献管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 400);
        setLayout(new BorderLayout(10, 10));

        // 标题区域
        JLabel titleLabel = new JLabel("专利文献管理系统");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        add(titleLabel, BorderLayout.NORTH);

        // 输入区域
        JPanel inputPanel = createInputPanel();
        add(inputPanel, BorderLayout.CENTER);

        // 按钮区域
        JPanel buttonPanel = createButtonPanel();
        add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    private JPanel createInputPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));

        // 申请号
        panel.add(createInputRow("申请号：", applicationNoField = new JTextField(20)));
        panel.add(Box.createVerticalStrut(10));

        // 申请日期
        panel.add(createInputRow("申请日期：", applicationDateField = new JTextField(20)));
        panel.add(Box.createVerticalStrut(10));

        // IPC分类号
        panel.add(createInputRow("IPC分类号：", ipcField = new JTextField(20)));
        panel.add(Box.createVerticalStrut(10));

        // CPC分类号
        panel.add(createInputRow("CPC分类号：", cpcField = new JTextField(20)));
        panel.add(Box.createVerticalStrut(10));

        // CPC位置和值
        JPanel cpcPosValuePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        cpcPosValuePanel.add(new JLabel("CPC位置："));
        cpcPositionField = new JTextField(2);
        cpcPosValuePanel.add(cpcPositionField);
        cpcPosValuePanel.add(Box.createHorizontalStrut(20));
        cpcPosValuePanel.add(new JLabel("值："));
        cpcValueField = new JTextField(2);
        cpcPosValuePanel.add(cpcValueField);
        panel.add(cpcPosValuePanel);

        return panel;
    }

    private JPanel createInputRow(String label, JTextField field) {
        JPanel panel = new JPanel(new BorderLayout(10, 0));
        panel.add(new JLabel(label), BorderLayout.WEST);
        panel.add(field, BorderLayout.CENTER);
        return panel;
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

        JButton addButton = createButton("添加", this::handleAdd);
        JButton queryButton = createButton("查询", this::handleQuery);
        JButton deleteButton = createButton("删除", this::handleDelete);

        panel.add(addButton);
        panel.add(queryButton);
        panel.add(deleteButton);

        return panel;
    }

    private JButton createButton(String text, java.awt.event.ActionListener listener) {
        JButton button = new JButton(text);
        button.setPreferredSize(new Dimension(100, 30));
        button.addActionListener(listener);
        return button;
    }

    // 事件处理方法
    private void handleAdd(ActionEvent e) {
        try {
            String appNo = applicationNoField.getText().trim();
            String appDate = applicationDateField.getText().trim();
            String ipcs = ipcField.getText().trim();
            String cpcs = cpcField.getText().trim();
            char pos = cpcPositionField.getText().trim().charAt(0);
            char val = cpcValueField.getText().trim().charAt(0);

            Patent patent = new Patent(appDate, ipcs, cpcs, pos, val);
            corpus.add(appNo, patent);

            clearFields();
            JOptionPane.showMessageDialog(this, "添加成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            showError("添加失败：" + ex.getMessage());
        }
    }

    private void handleQuery(ActionEvent e) {
        try {
            Corpus result = null;
            String appDate = applicationDateField.getText().trim();
            String ipc = ipcField.getText().trim();
            String cpc = cpcField.getText().trim();

            if (!appDate.isEmpty()) {
                result = corpus.find(new Date(appDate));
            } else if (!ipc.isEmpty()) {
                result = corpus.find(new Ipc(ipc));
            } else if (!cpc.isEmpty()) {
                char pos = cpcPositionField.getText().trim().charAt(0);
                char val = cpcValueField.getText().trim().charAt(0);
                result = corpus.find(new Cpc(cpc, pos, val));
            }

            showResult(result != null ? result.toString() : "未找到匹配结果");
        } catch (Exception ex) {
            showError("查询失败：" + ex.getMessage());
        }
    }

    private void handleDelete(ActionEvent e) {
        try {
            String appDate = applicationDateField.getText().trim();
            String ipc = ipcField.getText().trim();
            String cpc = cpcField.getText().trim();

            if (!appDate.isEmpty()) {
                corpus.remove(new Date(appDate));
            } else if (!ipc.isEmpty()) {
                corpus.remove(new Ipc(ipc));
            } else if (!cpc.isEmpty()) {
                char pos = cpcPositionField.getText().trim().charAt(0);
                char val = cpcValueField.getText().trim().charAt(0);
                corpus.remove(new Cpc(cpc, pos, val));
            }

            clearFields();
            JOptionPane.showMessageDialog(this, "删除操作完成", "提示", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            showError("删除失败：" + ex.getMessage());
        }
    }

    // 辅助方法
    private void clearFields() {
        applicationNoField.setText("");
        applicationDateField.setText("");
        ipcField.setText("");
        cpcField.setText("");
        cpcPositionField.setText("");
        cpcValueField.setText("");
    }

    private void showError(String message) {
        JOptionPane.showMessageDialog(this, message, "错误", JOptionPane.ERROR_MESSAGE);
    }

    private void showResult(String result) {
        JTextArea textArea = new JTextArea(15, 50);
        textArea.setText(result);
        textArea.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(textArea);
        JOptionPane.showMessageDialog(this, scrollPane, "查询结果", JOptionPane.PLAIN_MESSAGE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(PatentManagerUI::new);
    }
}